var bip39 = require('bip39')
var hdkey = require('ethereumjs-wallet/hdkey')
var util = require('ethereumjs-util')
var Tx = require('ethereumjs-tx');
var keythereum = require("keythereum");
var Web3 = require('web3');
if (typeof web3 !== 'undefined') {
  web3 = new Web3(web3.currentProvider);
} else {
  // set the provider you want from Web3.providers
  web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
var keythereum = require("keythereum");
// Specify a data directory (optional; defaults to ~/.ethereum)
var datadir = "E:\\geth\\testNet";

// Synchronous
var keyObject = keythereum.importFromFile("0x10a5fa2fFF03438941F262A660B4cD073524C205", datadir);
var privateKey = keythereum.recover("123456", keyObject);


var message = "xyz"
//var hasmsg = '0x' +web3.utils.sha3(message)
//var hasmsg = web3.utils.sha3('\u0019Ethereum Signed Message:\n' + message.length.toString()+message)
//console.log(util.toBuffer(hasmsg))
//var s = "\x19Ethereum Signed Message:\n" + message.length + message
const msgHash  = util.hashPersonalMessage(new Buffer(message))
var signAddress = util.ecsign(util.toBuffer(msgHash),privateKey)
console.log(signAddress)
console.log(signAddress.r.toString('hex')+signAddress.s.toString('hex'))

publicKey = util.secp256k1.publicKeyCreate(privateKey, false).slice(1);
console.log(publicKey.toString('hex'))
//0x9eda0abaa6ae0c7647a1a449f3903a53387b6f5375eb927775605601d201cc8577855a1f3be83f92d27e61e4989e9fdea0af4f16bd5f497065eee0b026ac1a0f1b
//fddf78da017d8cac8a825536e8a903d9c8fc8b51a4245e08414e3b905de900029f76f869f9e001111edfe82e77f10d0d6e52b858f1c6e789ae41d3e348bf08cc
//7f7d8e7f7045636a1c954dd19d83b0f045141e31159de1a9a4d85b9c86882abd1d7517f34a1c15ee
//4da1faf9a898a97667a4567647dc729b4adcc052be8f0ed4
//27